home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / os20 / wb / toolmanager2_0.lha / ToolManager / Source / library / handler.c < prev    next >
C/C++ Source or Header  |  1992-09-26  |  10KB  |  342 lines

  1. /*
  2.  * handler.c  V2.0
  3.  *
  4.  * handler main loop
  5.  *
  6.  * (c) 1990-1992 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* misc. data */
  12. static BOOL TimerOpen=FALSE;
  13. struct Library *DOSBase=NULL;
  14. struct Library *CxBase=NULL;
  15. struct Library *UtilityBase=NULL;
  16. struct IntuitionBase *IntuitionBase=NULL;
  17. struct Library *GfxBase=NULL;
  18. struct Library *GadToolsBase=NULL;
  19. extern const char DosName[];
  20. extern struct Library *LibBase;
  21. static struct NotifyRequest PrefsNotify={
  22.                                          PrefsFileName,NULL,0,
  23.                                          NRF_SEND_SIGNAL
  24.                                         };
  25. static ULONG NotifySignal=-1;
  26. static BOOL NotifyActive=FALSE;
  27.  
  28. /* Open handler resources */
  29. static BOOL OpenResources(void)
  30. {
  31.  if (!(LibraryPort=CreateMsgPort()))                       return(FALSE);
  32.  if (!(DOSBase=OpenLibrary(DosName,37)))                   return(FALSE);
  33.  if (!(CxBase=OpenLibrary("commodities.library",37)))      return(FALSE);
  34.  if (!(UtilityBase=OpenLibrary("utility.library",37)))     return(FALSE);
  35.  if (!(IntuitionBase=OpenLibrary("intuition.library",37))) return(FALSE);
  36.  if (!(GfxBase=OpenLibrary("graphics.library",37)))        return(FALSE);
  37.  if (!(GadToolsBase=OpenLibrary("gadtools.library",37)))   return(FALSE);
  38.  if (!(DummyPort=CreateMsgPort()))                         return(FALSE);
  39.  if (!(IDCMPPort=CreateMsgPort()))                         return(FALSE);
  40.  if (!(TimerPort=CreateMsgPort()))                         return(FALSE);
  41.  if (!(AppMsgPort=CreateMsgPort()))                        return(FALSE);
  42.  if (!(BrokerPort=CreateMsgPort()))                        return(FALSE);
  43.  if (!(deftimereq=CreateIORequest(TimerPort,sizeof(struct timerequest))))
  44.                                                            return(FALSE);
  45.  if (OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *) deftimereq,
  46.                 0))                                        return(FALSE);
  47.  TimerOpen=TRUE;
  48.  if (!(PrivateTMHandle=AllocMem(sizeof(struct TMHandle),MEMF_PUBLIC)))
  49.                                                            return(FALSE);
  50.  if (!(InternalAllocTMHandle(PrivateTMHandle)))            return(FALSE);
  51.  if ((NotifySignal=AllocSignal(-1))==-1)                   return(FALSE);
  52.  PrefsNotify.nr_stuff.nr_Signal.nr_Task=FindTask(NULL);
  53.  PrefsNotify.nr_stuff.nr_Signal.nr_SignalNum=NotifySignal;
  54.  if (!StartNotify(&PrefsNotify))                           return(FALSE);
  55.  NotifyActive=TRUE;
  56.  BrokerData.nb_Port=BrokerPort;
  57.  
  58.  /* Get locale stuff */
  59.  GetLocale();
  60.  
  61.  if (!(Broker=CxBroker(&BrokerData,NULL)))                 return(FALSE);
  62.  
  63.  /* Copy path from Workbench process */
  64.  {
  65.   struct Process *wbproc=FindTask("Workbench");
  66.  
  67.   /* Task found? Make sure it IS a process */
  68.   if (wbproc && (wbproc->pr_Task.tc_Node.ln_Type==NT_PROCESS)) {
  69.    /* It is a process */
  70.    struct CommandLineInterface *wbcli=BADDR(wbproc->pr_CLI);
  71.  
  72.    /* Make sure it is a CLI process */
  73.    if (wbcli) {
  74.     /* It is a CLI process */
  75.     struct PathList *dummy;
  76.  
  77.     /* Copy it's path into global path */
  78.     if (!CopyPathList(&GlobalPath,&dummy,
  79.                       (struct PathList *) BADDR(wbcli->cli_CommandDir)))
  80.      return(FALSE);
  81.    }
  82.   }
  83.  }
  84.  
  85.  /* All OK. */
  86.  return(TRUE);
  87. }
  88.  
  89. /* Free handler resources */
  90. static void FreeResources(void)
  91. {
  92.  FreePathList(GlobalPath); /* FreePath checks for NULL */
  93.  GlobalPath=NULL;
  94.  
  95.  if (Broker) {
  96.   DeleteCxObjAll(Broker);
  97.   Broker=NULL;
  98.  }
  99.  FreeLocale();
  100.  if (NotifyActive) {
  101.   EndNotify(&PrefsNotify);
  102.   NotifyActive=FALSE;
  103.  }
  104.  if (NotifySignal != -1) {
  105.   FreeSignal(NotifySignal);
  106.   NotifySignal=-1;
  107.  }
  108.  if (PrivateTMHandle) {
  109.   InternalFreeTMHandle(PrivateTMHandle);
  110.   FreeMem(PrivateTMHandle,sizeof(struct TMHandle));
  111.   PrivateTMHandle=NULL;
  112.  }
  113.  if (TimerOpen) {
  114.   CloseDevice((struct IORequest *) deftimereq);
  115.   TimerOpen=FALSE;
  116.  }
  117.  if (deftimereq) {
  118.   DeleteIORequest(deftimereq);
  119.   deftimereq=NULL;
  120.  }
  121.  if (BrokerPort) {
  122.   struct Message *msg;
  123.   while (msg=GetMsg(BrokerPort)) ReplyMsg(msg);
  124.   DeleteMsgPort(BrokerPort);
  125.   BrokerPort=NULL;
  126.  }
  127.  if (AppMsgPort) {
  128.   struct Message *msg;
  129.   while (msg=GetMsg(AppMsgPort)) ReplyMsg(msg);
  130.   DeleteMsgPort(AppMsgPort);
  131.   AppMsgPort=NULL;
  132.  }
  133.  if (TimerPort) {
  134.   DeleteMsgPort(TimerPort);
  135.   TimerPort=NULL;
  136.  }
  137.  if (IDCMPPort) {
  138.   DeleteMsgPort(IDCMPPort);
  139.   IDCMPPort=NULL;
  140.  }
  141.  if (DummyPort) {
  142.   DeleteMsgPort(DummyPort);
  143.   DummyPort=NULL;
  144.  }
  145.  if (GadToolsBase) {
  146.   CloseLibrary(GadToolsBase);
  147.   GadToolsBase=NULL;
  148.  }
  149.  if (GfxBase) {
  150.   CloseLibrary(GfxBase);
  151.   GfxBase=NULL;
  152.  }
  153.  if (IntuitionBase) {
  154.   CloseLibrary((struct Library *) IntuitionBase);
  155.   IntuitionBase=NULL;
  156.  }
  157.  if (UtilityBase) {
  158.   CloseLibrary(UtilityBase);
  159.   UtilityBase=NULL;
  160.  }
  161.  if (CxBase) {
  162.   CloseLibrary(CxBase);
  163.   CxBase=NULL;
  164.  }
  165.  if (DOSBase) {
  166.   CloseLibrary(DOSBase);
  167.   DOSBase=NULL;
  168.  }
  169.  if (LibraryPort) {
  170.   DeleteMsgPort(LibraryPort);
  171.   LibraryPort=NULL;
  172.  }
  173. }
  174.  
  175. /* handler main entry point */
  176. __geta4 void HandlerEntry(void)
  177. {
  178.  ULONG sigmask,lpsig,ipsig,tpsig,apsig,bpsig,npsig;
  179.  
  180.  /* Get resources */
  181.  if (!OpenResources()) {
  182.   FreeResources();
  183.   return;
  184.  }
  185.  
  186.  /* Read config */
  187.  if (!Closing) ReadConfig();
  188.  
  189.  /* Build signal masks */
  190.  lpsig=1L<<LibraryPort->mp_SigBit;
  191.  ipsig=1L<<IDCMPPort->mp_SigBit;
  192.  tpsig=1L<<TimerPort->mp_SigBit;
  193.  apsig=1L<<AppMsgPort->mp_SigBit;
  194.  bpsig=1L<<BrokerPort->mp_SigBit;
  195.  npsig=1L<<NotifySignal;
  196.  sigmask=ipsig|lpsig|tpsig|apsig|bpsig|npsig|SIGBREAKF_CTRL_F;
  197.  
  198.  /* Activate broker */
  199.  ActivateCxObj(Broker,TRUE);
  200.  
  201.  /* Wait until the end...*/
  202.  while (!Closing || (LibBase->lib_OpenCnt>0)) {
  203.   ULONG gotsigs;
  204.  
  205.   /* Wait for events */
  206.   DEBUG_PUTSTR("Handler: Waiting for messages...\n")
  207.   gotsigs=Wait(sigmask);
  208.  
  209.   /* Got an IDCMP event? (only dock objects!) */
  210.   if (gotsigs & ipsig) HandleIDCMPEvents();
  211.  
  212.   /* Got a timer event? */
  213.   if (gotsigs & tpsig) {
  214.    struct List *l=&TimerPort->mp_MsgList;
  215.    struct TMTimerReq *tr;
  216.  
  217.    /* Scan message list (special treatment for I/O Requests!) */
  218.    while (tr=GetHead(l)) CallActivateTMObject(tr->tmtr_Link,NULL);
  219.   }
  220.  
  221.   /* Got a Workbench App* message? */
  222.   if (gotsigs & apsig) {
  223.    struct AppMessage *msg;
  224.  
  225.    /* Empty message queue */
  226.    while (msg=GetMsg(AppMsgPort)) {
  227.     /* Activate object */
  228.     CallActivateTMObject((struct TMLink *) msg->am_ID,msg);
  229.  
  230.     /* Reply message */
  231.     ReplyMsg((struct Message *) msg);
  232.    }
  233.   }
  234.  
  235.   /* Got a Commodities event? */
  236.   if (gotsigs & bpsig) {
  237.    CxMsg *msg;
  238.  
  239.    /* Empty message queue */
  240.    while (msg=GetMsg(BrokerPort)) {
  241.  
  242.     DEBUG_PRINTF("Commodities event (0x%08lx)\n",CxMsgType(msg));
  243.  
  244.     /* What type of Commodities event? */
  245.     switch (CxMsgType(msg)) {
  246.      case CXM_IEVENT: /* Hotkey event --> Activate object */
  247.                      CallActivateTMObject((struct TMLink *) CxMsgID(msg),
  248.                                           NULL);
  249.                      break;
  250.  
  251.      case CXM_COMMAND: /* Commodities command */
  252.                       switch (CxMsgID(msg)) {
  253.                        case CXCMD_DISABLE: /* Disable broker */
  254.                                           ActivateCxObj(Broker,FALSE);
  255.                                           break;
  256.  
  257.                        case CXCMD_ENABLE:  /* Enable broker */
  258.                                           ActivateCxObj(Broker,TRUE);
  259.                                           break;
  260.  
  261.                        case CXCMD_KILL:    /* Quit application */
  262.                                           Closing=TRUE;
  263.                                           break;
  264.                        }
  265.                       break;
  266.     }
  267.  
  268.     /* Reply message */
  269.     ReplyMsg((struct Message *) msg);
  270.    }
  271.   }
  272.  
  273.   /* Got a command from the library interface? */
  274.   if (gotsigs & lpsig) {
  275.    struct TMHandle *handle;
  276.  
  277.    /* Empty message queue */
  278.    while (handle=GetMsg(LibraryPort)) {
  279.     BOOL rc=FALSE;
  280.  
  281.     DEBUG_PRINTF("Got command (%1lx)\n",handle->tmh_Command);
  282.  
  283.     /* What command did we receive? */
  284.     switch (handle->tmh_Command) {
  285.      case TMIPC_AllocTMHandle: rc=InternalAllocTMHandle(handle);
  286.                                break;
  287.  
  288.      case TMIPC_FreeTMHandle:  rc=InternalFreeTMHandle(handle);
  289.                                break;
  290.  
  291.      case TMIPC_CreateTMObject:
  292.                                rc=InternalCreateTMObject(handle,
  293.                                                          handle->tmh_Object,
  294.                                                          handle->tmh_Type,
  295.                                                          handle->tmh_Tags);
  296.                                break;
  297.  
  298.      case TMIPC_DeleteTMObject:rc=InternalDeleteTMObject(handle,
  299.                                                          handle->tmh_Object);
  300.                                break;
  301.  
  302.      case TMIPC_ChangeTMObject:rc=InternalChangeTMObject(handle,
  303.                                                          handle->tmh_Object,
  304.                                                          handle->tmh_Tags);
  305.                                break;
  306.     }
  307.  
  308.     /* Reply message */
  309.     handle->tmh_Command=rc;
  310.     DEBUG_PRINTF("Command reply (%1lx)\n",handle->tmh_Command);
  311.     ReplyMsg((struct Message *) handle);
  312.    }
  313.   }
  314.  
  315.   /* Got a configuration file notification event? */
  316.   if (gotsigs & npsig) {
  317.  
  318.    DEBUG_PRINTF("Preferences changed!\n");
  319.  
  320.    /* Free old config */
  321.    InternalFreeTMHandle(PrivateTMHandle);
  322.    FreeConfig();
  323.  
  324.    /* Read new config */
  325.    ReadConfig();
  326.   }
  327.  }
  328.  
  329.  /* Deactivate broker */
  330.  ActivateCxObj(Broker,FALSE);
  331.  
  332.  /* Free config buffers */
  333.  InternalFreeTMHandle(PrivateTMHandle);
  334.  FreeConfig();
  335.  
  336.  /* Free all resources &  Reset closing flag */
  337.  DEBUG_PUTSTR("Handler signing off...\n")
  338. /* Forbid(); */
  339.  FreeResources();
  340.  Closing=FALSE;
  341. }
  342.